﻿using System;
using System.Collections.Generic;
using System.Text;

namespace CPPEI.Coolzon.ViewState
{
    /// <summary>
    /// 视图状态
    /// </summary>
    [Serializable]
    public class ViewState
    {
        /// <summary>
        /// 当前的场景路径
        /// </summary>
        public ScenePath CurrentScene { get; private set; }

        /// <summary>
        /// 组件的状态列表
        /// 包括场景，区域，其他组件的状态
        /// </summary>
        public List<PartState> PartStateList { get; private set; }

        /// <summary>
        /// 时间长短，每次有修改之后会+1
        /// </summary>
        public long Age { get; private set; }

        /// <summary>
        /// 方案信息
        /// </summary>
        public string SolutionName { get; set; }

        private ViewState()
        {
            PartStateList = new List<PartState>();
        }

        #region scene path

        /// <summary>
        /// 打开场景
        /// </summary>
        /// <param name="sceneName"></param>
        /// <param name="arguments"></param>
        public void OpenScene(string sceneName, string arguments)
        {
            CurrentScene = new ScenePath("open", sceneName, arguments);
        }

        /// <summary>
        /// 弹出场景
        /// </summary>
        /// <param name="sceneName"></param>
        /// <param name="arguments"></param>
        public void PopScene(string sceneName, string arguments)
        {
            CurrentScene = new ScenePath("pop", sceneName, arguments, CurrentScene);
        }

        /// <summary>
        /// 关闭场景
        /// </summary>
        /// <param name="sceneName"></param>
        public void CloseScene(string sceneName)
        {
            if (CurrentScene.SceneName == sceneName)
            {
                CurrentScene = CurrentScene.Parent;
            }
        }

        #endregion

        #region part state

        public PartState GetPartState(string partFullName)
        {
            for (int index = 0; index < PartStateList.Count; index++)
            {
                if (PartStateList[index].Name == partFullName)
                {
                    return PartStateList[index];
                }
            }
            return null;
        }

        public void AddPartState(PartState partState)
        {
            PartStateList.Add(partState);
        }

        /// <summary>
        /// 删除组件的状态
        /// </summary>
        /// <param name="partFullName"></param>
        public void RemovePartState(string partFullName)
        {
            for (int index = 0; index < PartStateList.Count; index++)
            {
                if (PartStateList[index].Name == partFullName)
                {
                    PartStateList.RemoveAt(index);
                    return;
                }
            }
        }

        #endregion

        #region part property

        public void SetPartProperty(string partName, string propertyName, string value)
        {
            PartState partState = this.GetPartState(partName);
            if (partState == null)
            {
                partState = new PartState(partName);
                this.AddPartState(partState);
            }
            partState.SetString(propertyName, value);
        }

        #endregion

        public void Update()
        {
            Age++;
        }

        public void UpdateAge(long age)
        {
            Age = age;
        }

        /// <summary>
        /// age继续叠加，但是存储的组件状态都清空
        /// </summary>
        public void UpdateAndResetPartState()
        {
            Age++;
            CurrentScene = null;
            PartStateList.Clear();
        }

        #region static methods

        /// <summary>
        /// 转换为字符串
        /// </summary>
        /// <param name="state"></param>
        /// <returns></returns>
        public static string ToToken(ViewState state)
        {
            if (state == null)
            {
                return string.Empty;
            }
            // ScenePath
            // ex:
            //     open:s1?args1/pop:s2?args2
            // PartState
            // ex:
            //     p1?a1=2&a2=3#p2?a1=3
            string ageString = state.Age.ToString();
            string pathString = ScenePathUtil.ToString(state.CurrentScene);
            string partString = PartStateUtil.ToString(state.PartStateList);
            return string.Format("{0}|{1}|{2}|{3}", state.SolutionName, ageString, pathString, partString);
        }

        /// <summary>
        /// 从字符串转换为对象
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public static ViewState FromToken(string token)
        {
            if (string.IsNullOrEmpty(token))
            {
                return new ViewState();
            }
            ViewState viewState = new ViewState();
            string[] segments = token.Split(new char[] { '|' });
            viewState.SolutionName = segments[0];
            viewState.Age = Convert.ToInt64(segments[1]);
            viewState.CurrentScene = ScenePathUtil.FromString(segments[2]);
            viewState.PartStateList = PartStateUtil.FromString(segments[3]);
            return viewState;
        }

        /// <summary>
        /// 获取一个空的视图状态
        /// </summary>
        /// <returns></returns>
        public static ViewState Empty()
        {
            return new ViewState();
        }

        #endregion
    }
}
